<?php

if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

/**
 * 信息列表模块
 */
class Info extends MY_Controller
{
    
    public function rentGet( $type = 'all', $offset = 0 )
    {
        //信息总数
        $total = $this->model_post_rentTotal();
        $perPage = config_item('per_page');
        $pageConfig = array(
            'base_url' => my_url( 'rent', 'index', $type, false),
            'total_rows' => $total,
            'per_page' => $perPage,
        );
        $this->pagination->initialize( $pageConfig );
        $pagination = $this->pagination->create_links();
        
        if ( $type == 'personal' )
        {
            //个人房源
            $infoData = $this->model_post_rentPersonal( $perPage, $offset );
        }
        else if( $type == 'agent' )
        {
            //中介
            $infoData = $this->model_post_rentAgent( $perPage, $offset );
        }
        else
        {
            //所有
            $infoData = $this->model_post_rentAll( $perPage, $offset );
        }
        
        $data = array( 
            'pagination' => $pagination,
            'infoData' => $infoData,
            'type' => $type,
        );
        $this->load->view( 'info_list', $data );
    }
    
    public function sellGet( $type = 'all', $offset = 0 )
    {
        //信息总数
        $total = $this->model_post_rentTotal();
        $perPage = config_item('per_page');
        $pageConfig = array(
            'base_url' => my_url( 'sell', 'index', $type, false),
            'total_rows' => $total,
            'per_page' => $perPage,
        );
        $this->pagination->initialize( $pageConfig );
        $pagination = $this->pagination->create_links();
        
        if ( $type == 'personal' )
        {
            //个人房源
            $infoData = $this->model_post_sellPersonal( $perPage, $offset );
        }
        else if( $type == 'agent' )
        {
            //中介
            $infoData = $this->model_post_sellAgent( $perPage, $offset );
        }
        else
        {
            //所有
            $infoData = $this->model_post_sellAll( $perPage, $offset );
        }
        
        $data = array( 
            'pagination' => $pagination,
            'infoData' => $infoData,
            'type' => $type,
        );
        $this->load->view( 'info_list', $data );
    }

    /**
     * 根据地区ID获取信息
     * @param int $zoneID 区域ID
     * @param string $type 类型
     * @param int $offset 游标
     */
    public function rentZone( $zoneID, $type = 'all', $offset = 0 )
    {
        //获取区域数据
        $zoneData = $this->model_common_getZoneDataById( $zoneID );

        //获取该地区的租房信息总数
        $total = $this->model_post_rentTotalByZoneId( $zoneID );
        $perPage = config_item('per_page');
        $pageConfig = array(
            'base_url' => my_url( 'rent', 'zone', $zoneID, false),
            'total_rows' => $total,
            'per_page' => $perPage,
        );
        $this->pagination->initialize( $pageConfig );
        $pagination = $this->pagination->create_links();
        
        if ( $type == 'personal' )
        {
            //个人房源
            $infoData = $this->model_post_rentPersonalByZoneId( $zoneID, $perPage, $offset );
        }
        else if( $type == 'agent' )
        {
            //中介
            $infoData = $this->model_post_rentAgentByZoneId( $zoneID, $perPage, $offset );
        }
        else
        {
            //所有
            $infoData = $this->model_post_rentAllByZoneId( $zoneID, $perPage, $offset );
        }
        
        $data = array( 
            'pagination' => $pagination,
            'infoData' => $infoData,
            'type' => $type,
            'zoneData' => $zoneData,
        );
        $this->load->view( 'info_zone', $data );
    }
    
    /**
     * 根据地区ID获取信息
     * @param int $zoneID 区域ID
     * @param string $type 类型
     * @param int $offset 游标
     */
    public function sellZone( $zoneID, $type = 'all', $offset = 0 )
    {
        //获取区域数据
        $zoneData = $this->model_common_getZoneDataById( $zoneID );

        //获取该地区的租房信息总数
        $total = $this->model_post_sellTotalByZoneId( $zoneID );
        $perPage = config_item('per_page');
        $pageConfig = array(
            'base_url' => my_url( 'sell', 'zone', $zoneID, false),
            'total_rows' => $total,
            'per_page' => $perPage,
        );
        $this->pagination->initialize( $pageConfig );
        $pagination = $this->pagination->create_links();
        
        if ( $type == 'personal' )
        {
            //个人房源
            $infoData = $this->model_post_sellPersonalByZoneId( $zoneID, $perPage, $offset );
        }
        else if( $type == 'agent' )
        {
            //中介
            $infoData = $this->model_post_sellAgentByZoneId( $zoneID, $perPage, $offset );
        }
        else
        {
            //所有
            $infoData = $this->model_post_sellAllByZoneId( $zoneID, $perPage, $offset );
        }
        
        $data = array( 
            'pagination' => $pagination,
            'infoData' => $infoData,
            'type' => $type,
            'zoneData' => $zoneData,
        );
        $this->load->view( 'info_zone', $data );
    }
    
    /**
     * 根据厅室类型ID获取信息
     * @param int $roomID 类型ID
     * @param string $type 类型
     * @param int $offset 游标
     */
    public function sellRoom( $roomID, $type = 'all', $offset = 0 )
    {
        //获取厅室数据
        $roomData = $this->model_common_getRoomDataById( $roomID );

        //获取该地区的租房信息总数
        $total = $this->model_post_sellTotalByRoomId( $roomID );
        $perPage = config_item('per_page');
        $pageConfig = array(
            'base_url' => my_url( 'sell', 'room', $roomID, false),
            'total_rows' => $total,
            'per_page' => $perPage,
        );
        $this->pagination->initialize( $pageConfig );
        $pagination = $this->pagination->create_links();
        
        if ( $type == 'personal' )
        {
            //个人房源
            $infoData = $this->model_post_sellPersonalByRoomId( $roomID, $perPage, $offset );
        }
        else if( $type == 'agent' )
        {
            //中介
            $infoData = $this->model_post_sellAgentByRoomId( $roomID, $perPage, $offset );
        }
        else
        {
            //所有
            $infoData = $this->model_post_sellAllByRoomId( $roomID, $perPage, $offset );
        }
        
        $data = array( 
            'pagination' => $pagination,
            'infoData' => $infoData,
            'type' => $type,
            'roomData' => $roomData,
        );
        $this->load->view( 'info_room', $data );
    }
    
    /**
     * 根据价格范围查询数据
     * @param int $lowPrice 价格下限
     * @param int $highPrice 价格上限
     * @param string $type 类型
     * @param int $offset 游标
     */
    public function rentPrice( $lowPrice, $highPrice, $type = 'all', $offset = 0 )
    {
        //获取该金额范围的租房信息总数
        $priceRange = ($highPrice == 'max') ? "{$lowPrice}以上" : "{$lowPrice}-{$highPrice}";
        $highPrice = ($highPrice == 'max') ? '' : $highPrice;
        $total = $this->model_post_rentTotalByPrice( $lowPrice, $highPrice );
        $perPage = config_item('per_page');
        $pageConfig = array(
            'base_url' => my_url( 'rent', 'price', $priceRange, false),
            'total_rows' => $total,
            'per_page' => $perPage,
        );
        $this->pagination->initialize( $pageConfig );
        $pagination = $this->pagination->create_links();
        
        if ( $type == 'personal' )
        {
            //个人房源
            $infoData = $this->model_post_rentPersonalByPrice( $lowPrice, $highPrice, $perPage, $offset );
        }
        else if( $type == 'agent' )
        {
            //中介
            $infoData = $this->model_post_rentAgentByPrice( $lowPrice, $highPrice, $perPage, $offset );
        }
        else
        {
            //所有
            $infoData = $this->model_post_rentAllByPrice( $lowPrice, $highPrice, $perPage, $offset );
        }
        
        $data = array( 
            'pagination' => $pagination,
            'infoData' => $infoData,
            'type' => $type,
            'priceRange' => $priceRange,
        );
        $this->load->view( 'info_price', $data );
    }
    
    /**
     * 根据价格范围查询数据
     * @param int $lowPrice 价格下限
     * @param int $highPrice 价格上限
     * @param string $type 类型
     * @param int $offset 游标
     */
    public function sellPrice( $lowPrice, $highPrice, $type = 'all', $offset = 0 )
    {
        //获取该金额范围的租房信息总数
        $priceRange = ($highPrice == 'max') ? "{$lowPrice}以上" : "{$lowPrice}-{$highPrice}";
        $highPrice = ($highPrice == 'max') ? '' : $highPrice;
        $total = $this->model_post_sellTotalByPrice( $lowPrice, $highPrice );
        $perPage = config_item('per_page');
        $pageConfig = array(
            'base_url' => my_url( 'sell', 'price', $priceRange, false),
            'total_rows' => $total,
            'per_page' => $perPage,
        );
        $this->pagination->initialize( $pageConfig );
        $pagination = $this->pagination->create_links();
        
        if ( $type == 'personal' )
        {
            //个人房源
            $infoData = $this->model_post_sellPersonalByPrice( $lowPrice, $highPrice, $perPage, $offset );
        }
        else if( $type == 'agent' )
        {
            //中介
            $infoData = $this->model_post_sellAgentByPrice( $lowPrice, $highPrice, $perPage, $offset );
        }
        else
        {
            //所有
            $infoData = $this->model_post_sellAllByPrice( $lowPrice, $highPrice, $perPage, $offset );
        }
        
        $data = array( 
            'pagination' => $pagination,
            'infoData' => $infoData,
            'type' => $type,
            'priceRange' => $priceRange,
        );
        $this->load->view( 'info_price', $data );
    }
    
    /**
     * 搜索
     * @param string $keyword
     * @param int $offset 
     */
    public function search( $keyword = '', $offset = 0 )
    {
        //信息总数
        $total = $keyword ? $this->model_post_rentTotalByKeyword( $keyword ) : $this->model_post_rentTotal();
        $perPage = config_item('per_page');
        $pageConfig = array(
            'base_url' => my_url( 'query', 'index', '?', false),
            'total_rows' => $total,
            'per_page' => $perPage,
            'page_query_string' => true,
            'query_string_segment' => 'p',
        );
        $this->pagination->initialize( $pageConfig );
        $pagination = $this->pagination->create_links();
        
        if ( $keyword )
        {
            //关键字搜索
            $infoData = $this->model_post_rentGetByKeyword( $keyword, $perPage, $offset );
        }
        else
        {
            //所有
            $infoData = $this->model_post_rentAll( $perPage, $offset );
        }
        
        $data = array( 
            'pagination' => $pagination,
            'infoData' => $infoData,
            'keyword' => $keyword,
            'total' => $total,
        );
        $this->load->view( 'info_search', $data );
    }
    
    /**
     * 获取用户发布的信息
     * @param int $offset
     */
    public function userInfo( $offset = 0 )
    {
        $data = array();
        //当前操作人ID
        $userId = $this->session->getUserSession('id');
        
        $total = $this->model_post_totalByUserId( $userId );
        $perPage = config_item('per_page');
        $pageConfig = array(
            'base_url' => my_url( 'info', 'index', '', false),
            'total_rows' => $total,
            'per_page' => $perPage,
        );
        $this->pagination->initialize( $pageConfig );
        $pagination = $this->pagination->create_links();
        $data['pagination'] = $pagination;
        
        $infoData = $this->model_post_getByUserId( $userId );
        $data['infoData'] = $infoData;
        
        $this->load->view( 'info_userlist', $data );
    }
}